<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/1/13 0013
 * Time: 11:36
 */

namespace app\printer\controller;

//客户信息
use think\Db;
use think\facade\Request;

class CustomerController extends BaseController
{
    public function index()
    {
        return $this->fetch();
    }

    //得到数据展示到表格
    public function getData()
    {
        $page = Request::get('page', 0);
        $limit = Request::get('limit', 15);
        $data = Db::name('customer')->page($page, $limit)->order(['id'=>'desc','create_time'=>'desc'])->select();
        $count = Db::name('customer') ->count();

        return ['code' => 0,'count'=> $count, 'data' => $data];
    }

    //添加
    public function add()
    {
        if(Request::isPost()){
            $post_data = Request::post();
            $data = [];
            foreach ($post_data as $key => $post_datum) {
                foreach ($post_datum as $index => $item) {
                    if(empty($post_data['user_name'][$index])) continue;
                    $data [$index]= [
                        'user_name' => $post_data['user_name'][$index],
                        'tel' => $post_data['tel'][$index],
                        'address' => $post_data['address'][$index],
                        'comment' => $post_data['comment'][$index],
                        'unit_name' => $post_data['unit_name'][$index],
                        'duty_num' => $post_data['duty_num'][$index],
                        'pay_period' => $post_data['pay_period'][$index],
                        'create_time' => getDateTime()
                    ];
                }
            }
            $res = Db::name('customer')->insertAll($data);
            if($res){
                return $this->ajaxSuccess();
            }else{
                return $this->ajaxFail();
            }
        }
        echo  $this->fetch();
    }

    //删除
    public function del()
    {
        $id  = Request::param('id');
        $res = Db::name('customer')->delete($id);
        if($res){
            return $this->ajaxSuccess();
        }else{
            return $this->ajaxFail();
        }
    }
}